package com.lz.kunkun.messageservice.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.lz.kunkun.messageservice.ServicesManager;
import com.lz.kunkun.messageservice.model.BaseResult;
import com.lz.kunkun.messageservice.model.ContactPeople;
import com.lz.kunkun.messageservice.model.SysUser;
import com.lz.kunkun.messageservice.service.IContactPeopleService;
import com.lz.kunkun.messageservice.util.FileUtil;

/**
 * Hello world!
 *
 */
@RestController
@RequestMapping("contact/")
public class ContactPeopleController {
	//获取联系人服务
	public IContactPeopleService getContactPeopleService() {
		return ServicesManager.instance.getIContactUserService();
	}
	//获取单个联系人信息
	@GetMapping("getPeople/{peopleId}")
	public @ResponseBody ContactPeople getPeople(HttpServletRequest req, @PathVariable Integer peopleId) {
		return getContactPeopleService().getPeople(peopleId,((SysUser)req.getSession().getAttribute("sysUser")).getUserId());
	}
	//添加一条联系人信息
	@PostMapping("addPeople")
	public @ResponseBody BaseResult addPeople(HttpServletRequest req,ContactPeople people,MultipartFile file) {
		Integer userId = ((SysUser) req.getSession().getAttribute("sysUser")).getUserId();
		if(0==getContactPeopleService().getPeopleByPhone(userId, people.getPhoneNumber())){
			people.setImg(FileUtil.fileSave(file));
			int status=getContactPeopleService().addPeople(people,userId);
			String mes=status==1?"添加成功！":"系統异常！添加失败！";
			return new BaseResult(status,mes);
		}
		return new BaseResult(0,people.getPhoneNumber()+"：号码已存在");
	}
	//删除某个联系人
	@GetMapping("delPeople/{peopleId}")
	public @ResponseBody BaseResult delPeople(HttpServletRequest req,@PathVariable Integer peopleId) {	
		int status=getContactPeopleService().delPeople(peopleId,((SysUser)req.getSession().getAttribute("sysUser")).getUserId());
		String mes=status==1?"删除成功！":"系統异常！删除失败！";
		return new BaseResult(status,mes);
	}
	//获取所有联系人
	@GetMapping("allPeople")
	public @ResponseBody List<ContactPeople> allPeople(HttpServletRequest req,String keyword,String orderBy) {
		return getContactPeopleService().allPeople(((SysUser)req.getSession().getAttribute("sysUser")).getUserId(),keyword,orderBy);
	}
	//更新联系人信息
	@PostMapping("updatePeople")
	public @ResponseBody BaseResult updatePeople(HttpServletRequest req,ContactPeople people,MultipartFile img) {
		int status=getContactPeopleService().updatePeople(people,((SysUser)req.getSession().getAttribute("sysUser")).getUserId());
		String mes=status==1?"更新成功！":"系統异常！更新失败！";
		return new BaseResult(status,mes);
	}
}
